package com.voute.Presenter.Impl;

import com.voute.Model.Bean.BaseBean;
import com.voute.Model.Bean.SimpleCircleInfoBean;
import com.voute.Model.ICircleModel;
import com.voute.Model.Impl.CircleModelImpl;
import com.voute.Presenter.ISimpleCirclePresenter;
import com.voute.Utils.CommonSubscriber;
import com.voute.Utils.TransformUtils;
import com.voute.View.IView.ICirclesListView;

import rx.functions.Action0;

/**
 * Created by xianke on 22/04/2017.
 */

public class SimpleCirclePresenterImpl implements ISimpleCirclePresenter {

    private ICirclesListView view;
    private ICircleModel model;

    public SimpleCirclePresenterImpl(ICirclesListView circleView) {
        if (circleView != null) {
            view = circleView;
            model = new CircleModelImpl();
        }
    }

    @Override
    public void requestCircles(String anchor) {
        model.requestCircles(anchor).compose(TransformUtils.<SimpleCircleInfoBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                view.showLoading();
            }
        }).subscribe(new CommonSubscriber<SimpleCircleInfoBean>() {
            @Override
            public void onSuccess(SimpleCircleInfoBean response) {
                view.hideLoading();
                view.refreshCirclesList(response);
            }

            @Override
            public void onFailure(BaseBean response) {
                view.hideLoading();
                view.showError(response.getMsg());
            }
        });
    }
}
